import { _decorator, Component, Label, Node, Sprite } from 'cc';
import { DEBUG } from 'cc/env';
import { games, lobby, players, yy } from 'yycore';

const { ccclass, property } = _decorator;

@ccclass('UserExpLvInfo')
export class UserExpLvInfo extends Component {

    // vip等级进度条星星中的文本
    @property(Label)
    private readonly txtExpLv: Label = null;
    // vip进度条前景图
    @property(Sprite)
    private readonly expFgBar: Sprite = null;

    protected onLoad(): void {
        // 监听玩家经验更新事件
        players.myself.on(this, players.events.ExpChanged, this._onPlayerExperanceChanged);

        if (this.expFgBar) {
            this.expFgBar.type = Sprite.Type.FILLED;
            this.expFgBar.fillType = Sprite.FillType.HORIZONTAL;
            this.expFgBar.fillStart = 0;
        }
    }

    protected start(): void {
        this._onPlayerExperanceChanged();
    }

    // 玩家等级经验变化事件
    private _onPlayerExperanceChanged(e?: players.events.ExpChanged) {
        DEBUG && console.log('# players.events.ExpChanged', e);
        const exInfo: games.ExpLevelInfo = players.myself.getExpLevel();
        // 经验等级
        if (this.txtExpLv)
            this.txtExpLv.string = exInfo.level.toString();
        // 当前等级的经验进度
        const precent: number = exInfo.lastLeft / (exInfo.nextLevelNeed || 1);
        if (this.expFgBar)
            this.expFgBar.fillRange = precent;
    }

}

